*&---------------------------------------------------------------------*
*& Report ZEX_LISTING_214                                              *
*&---------------------------------------------------------------------*
*& Created By: James Wood (james.wood@bowdarkconsulting.com)           *
*& Created On: 12/12/2008                                              *
*& Purpose:    This program demonstrates the Point API described in    *
*&             Listing 2.14 and Listing 2.15.                          *
*&---------------------------------------------------------------------*
REPORT zex_listing_214.

*----------------------------------------------------------------------*
*       CLASS lcl_point DEFINITION
*----------------------------------------------------------------------*
CLASS lcl_point DEFINITION.

  PUBLIC SECTION.
    DATA: x TYPE i, "X-Coordinate
          y TYPE i. "Y-Coordinate

    METHODS get_distance IMPORTING im_point2
                          TYPE REF TO lcl_point
                         EXPORTING ex_distance
                              TYPE f.

ENDCLASS.                    "lcl_point DEFINITION

*----------------------------------------------------------------------*
*       CLASS lcl_point IMPLEMENTATION
*----------------------------------------------------------------------*
CLASS lcl_point IMPLEMENTATION.

  METHOD get_distance.
*   Method-Local Data Declarations:
    DATA: lv_dx TYPE i, "Diff. X
          lv_dy TYPE i. "Diff. Y

*   Calculate the Euclidean distance between the points:
    lv_dx = im_point2->x - me->x.
    lv_dy = im_point2->y - me->y.

    ex_distance =
      SQRT( ( lv_dx * lv_dx ) + ( lv_dy * lv_dy ) ).
  ENDMETHOD.                    "get_distance

ENDCLASS.                    "lcl_point IMPLEMENTATION

*&---------------------------------------------------------------------*
*& START-OF-SELECTION Event                                            *
*&---------------------------------------------------------------------*
START-OF-SELECTION.
  PERFORM test_point_class.

*&---------------------------------------------------------------------*
*&      Form  test_point_class
*&---------------------------------------------------------------------*
FORM test_point_class.
* Local Data Declarations:
  DATA: lr_point_a  TYPE REF TO lcl_point,
        lr_point_b  TYPE REF TO lcl_point,
        lv_distance TYPE f.

  CREATE OBJECT lr_point_a.
  lr_point_a->x = 2.
  lr_point_a->y = 2.

  CREATE OBJECT lr_point_b.
  lr_point_b->x = 4.
  lr_point_b->y = 4.

  CALL METHOD lr_point_a->get_distance
    EXPORTING
      im_point2   = lr_point_b
    IMPORTING
      ex_distance = lv_distance.

  WRITE: / 'Distance between point a and b is:', lv_distance.
ENDFORM.                    "test_point_class